package com.example.demo.controller;

import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.*;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Slf4j
@RequestMapping("/cycle")
@RestController
public class CycleController {
    @GetMapping("/start")
    public String start(String userId) {
        String msg = "";
        //1.得到ProcessEngine对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //2.得到RunService对象
        RuntimeService runtimeService = processEngine.getRuntimeService();
        String instanceKey = "cycle";

        ProcessInstance instance = runtimeService.startProcessInstanceByKey(instanceKey);
        TaskService taskService = processEngine.getTaskService();
        String instanceId = instance.getId(); // 任务ID
        Task task = taskService.createTaskQuery().processInstanceId(instanceId).singleResult();
        String name = task.getName();
        msg = msg + "当前状态:" + name + "\n流程实例ID:" + instance.getId() + "\n";
        log.info(msg);
//        log.info("启动流程实例成功:"+ instance);
//        log.info("流程实例ID:"+instance.getId());
//        log.info("流程定义ID:"+instance.getProcessDefinitionId());
        return msg;
    }

    @GetMapping("audit")
    public String audit(String id) {
        String msg = "";
        //1.得到ProcessEngine对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //2.得到RunService对象
        TaskService taskService = processEngine.getTaskService();
        String instanceId = id; // 任务ID
        Task task = taskService.createTaskQuery().processInstanceId(instanceId).singleResult();
        if (task == null) {
            return "任务结束或不存在";
        }
        String preName = task.getName();
        taskService.complete(task.getId());
        Task postTask = taskService.createTaskQuery().processInstanceId(instanceId).singleResult();
        String postName = postTask == null ? "" : postTask.getName();
        msg += "状态变化：" + preName + "->" + postName;
        log.info(msg);
        return msg;
    }

    @GetMapping("/showList2")
    public String showList2(String id) {
        String instanceId = id; // 任务ID
        String msg = "";
        //1.得到ProcessEngine对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //2.得到RunService对象
        TaskService taskService = processEngine.getTaskService();
        HistoryService historyService = processEngine.getHistoryService();
        List<HistoricActivityInstance> list = historyService.createHistoricActivityInstanceQuery().processInstanceId(instanceId).finished().orderByHistoricActivityInstanceStartTime().asc().list();

        for (HistoricActivityInstance instance : list) {
            String taskId = instance.getTaskId();
            String name = instance.getActivityName();

            msg += "name:" + name + "\n";
        }
        Task task = taskService.createTaskQuery().processInstanceId(instanceId).singleResult();
        if(task!=null){
            msg+="当前状态："+task.getName();
        }
        return msg;
    }
}
